package com.dcivision.framework.web;

/*
 * @(#)PreservingUrlHttpServletRequestWrapper.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * This wrapper can be used to preserve the URI/URL of the originaly request.
 * It will return the URI/URL of the HttpRequest originated from the user's browser if they are preserved. 
 * This is useful when the webapp use various MVC framework and different Template engine. 
 *
 * @author Ferdinand Chan
 * @company DCIVision Limited
 * @creation date 09/11/2005
 * @version $Revision: 1.2 $
 */
public class PreservingUrlHttpServletRequestWrapper extends
		HttpServletRequestWrapper {

	/**
	 * The preserved URI
	 */
	private String preservedRequestUri;
	
	/**
	 * The preserved URL
	 */
	private StringBuffer preservedRequestUrl;
	
	/**
	 * Flag that indicate the preservation of URI
	 */
	private boolean preserveUri;
	
	/**
	 * Flag that indicate the preservation of URL
	 */
	private boolean preserveUrl;
	
	/**
	 * Constructor for constructing the wrapper.
	 * The uri of the request will be preserved during the construction phase
	 * @param request The original HttpServletRequest
	 * @param preserveUri Flag controlling whether the URI should be preserved or not
	 * @param preserveUrl Flag controlling whether the URL should be preserved or not
	 */
	public PreservingUrlHttpServletRequestWrapper(HttpServletRequest request,boolean preserveUri,boolean preserveUrl){
		super(request);
		
		//Save the flags
		this.preserveUri = preserveUri;
		this.preserveUrl = preserveUrl;
		
		//Preserve the original request uri and url
		preservedRequestUri = request.getRequestURI();
		preservedRequestUrl = request.getRequestURL();
	}

	/**
	 * Override the getRequestURI method to return the preserved URI if the preserveUri flag is on
	 * or return the URI from the request if the preserveUri flag is off  
	 */
	public String getRequestURI() {		
		return preserveUri ? preservedRequestUri : super.getRequestURI();
	}

	/**
	 * Override the getRequestURL method to return the preserved URL if the preserveUrl flag is on
	 * or return the URL from the request if the preserveUrl flag is off  
	 */
	public StringBuffer getRequestURL() {
		return preserveUrl ? preservedRequestUrl : super.getRequestURL();
	}
	
	
}
